﻿using System;
using System.Collections.Generic;
using AFDS.BusinessLogic;
using AFDS.BusinessLogic.Helpers;
using AFDS.Core;

namespace AFDS.BusinessLogic.Comparators
{
    public class CategoryComparer : IComparer<Category>, IDisposable
    {
        private SortFactor _sortFactor;
        private OrderBy _orderBy;
        private CategoryHelper cHelper;

        #region Constructors
        public CategoryComparer()
        {
            sortFactor = SortFactor.ASC;
            orderBy = OrderBy.Name;
            ToMakeHelpers();
        }

        public CategoryComparer(SortFactor sFactor)
        {
            sortFactor = sFactor;
            orderBy = OrderBy.Name;
            ToMakeHelpers();
        }

        public CategoryComparer(SortFactor sFactor, OrderBy oBy)
        {
            sortFactor = sFactor;
            orderBy = oBy;
            ToMakeHelpers();
        }
        #endregion

        #region Propierties

        public OrderBy orderBy
        {
            get
            {
                return _orderBy;
            }
            set
            {
                _orderBy = value;
            }
        }

        public SortFactor sortFactor
        {
            get
            {
                return _sortFactor;
            }
            set
            {
                _sortFactor = value;
            }
        }

        #endregion

        #region IComparer<Category> Members

        public int Compare(Category x, Category y)
        {
            int result = 0;
            if (orderBy == OrderBy.ParentName)
            {
                Guid rooParentId = new Guid("00000000-0000-0000-0000-000000000000");
                if (x.ParentId != rooParentId && y.ParentId != rooParentId)
                {
                    Category xParent = cHelper.Load(x.ParentId);
                    Category yParent = cHelper.Load(y.ParentId);
                    if (xParent.Name == yParent.Name)
                        result = x.Name.CompareTo(y.Name);
                    else
                        result = xParent.Name.CompareTo(yParent.Name);
                }
                else
                {
                    result = 0;
                }
            }
            else
            {
                if (x.Name == y.Name)
                    result = 0;
                else
                    result = x.CompareTo(y);
            }

            if (sortFactor == SortFactor.DESC)
                result = result * (-1);

            return result;
        }

        #endregion


        #region IDisposable Members

        public void Dispose()
        {
            cHelper.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        private void ToMakeHelpers()
        {
            cHelper = new CategoryHelper();
        }
    }

    public enum OrderBy
    {
        ParentName, Name
    }
}